package com.zsrt.devbase.common.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class PersonCodeUtil {
	
	/**
	 * 一位特定大写随机英文字典中抽一位+uid5位+rand(二位数)+time(月日时分8位),
	 * 然后截取16位出来生成比如T132-0161-2052-3091,
	 * 有些超市用户一个月就分发百万张出去,没有重复的反馈.
	 * 之所以加英文,主要还是增加接口查询被随机碰撞破解的难度.
	 * @return
	 */
	public static String getPersonCode(){
		//随机任意一位大写字母1
		String word=getFirstWordForPersonCode();
		//截取uuid5
		String uuid=StringUtil.getUUID().substring(1, 6);
		//随机2为数字
		String num=getTwoNumForPersonCode();
		//当前日期的月日时分8
		String time=getTimeForPersonCode();
		//生成personCode
		String s=word+uuid.substring(0, 3)+uuid.substring(3,5)+num+time.substring(0, 4)+time.substring(4,8);
		return s;
	}
	/**
	 * 当前时间的月日时分
	 * @return
	 */
	private static String getTimeForPersonCode(){
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("MMddHHmm");
		String time=format.format(date);
		return time;
	}
	/**
	 * 随机一位大写字母
	 * @return
	 */
	private static String getFirstWordForPersonCode(){
		final int maxNum = 26;
		int i;
		Random r = new Random();
		String[] str = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
				"T", "U", "V", "W", "X", "Y", "Z"};
		i = Math.abs(r.nextInt(maxNum));
		String word=str[i];
		return word;
	}
	/**
	 * 随机生成两位数字
	 * @return
	 */
	private static String getTwoNumForPersonCode(){
		int len=2;
		String sb="";
		for(int index=0;index<len;index++) {
			String s = String.valueOf((int)(10*(Math.random())));
			sb+=s;
		}
		return sb;
	}
}
